package com.zlkj.pro.core.model.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * 返回分页结果
 *
 * @author 孙铭泽
 * @date 2020-10-27
 * @time 22:23
 **/
@Getter
@Setter
public class PageResult<T> implements Serializable {
    /**
     * 结果集
     */
    private List<T> results;
    /**
     * 总条数
     */
    private Long total;
    /**
     * 总页数
     */
    private Long pages;

    /**
     * 当前条数
     */
    private Long size;
    /**
     * 当前页数
     */
    private Long current;

    /**
     * @param iPage   分页后对象
     * @param results 分页结果集
     */
    public PageResult(IPage<?> iPage, List<T> results) {
        this.results = results;
        this.total = iPage.getTotal();
        this.size = iPage.getSize();
        this.current = iPage.getCurrent();
        this.pages = iPage.getPages();
    }

    public PageResult(List<T> results, Long total, Long size, Long current) {
        this.results = results;
        this.total = total;
        this.size = size;
        this.current = current;
    }

    /**
     * 当前分页总页数
     */
    public long getPages() {
        if (getSize() == 0) {
            return 0L;
        }
        long pages = getTotal() / getSize();
        if (getTotal() % getSize() != 0) {
            pages++;
        }
        return pages;
    }
}
